package org.mybatis.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.mybatis.model.User;

import com.google.common.collect.Lists;

@SuppressWarnings("unused") public class ConfigApplication {
    private static Logger logger;
    static{
        logger = Logger.getLogger(Object.class);
    }
    
    @SuppressWarnings("rawtypes")
    private final static ConfigApplication instance = new ConfigApplication();
    
    public static ConfigApplication getInstance() {
        return instance;
    }
    
    public SqlSessionFactory sessionFactory() {
        String resource = "config/mybatis-config.xml";
        InputStream config = null;
        SqlSessionFactory sqlSessionFactory = null;
        try{
            config = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
        }
        catch (IOException e){
            e.printStackTrace();
        }
        finally{
            if (config != null){
                try{
                    config.close();
                }
                catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return sqlSessionFactory;
    }
    
    public SqlSession sqlSession() {
        SqlSessionFactory sqlSessionFactory = sessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
    
    public <T, E> void removeElement(List<T> element, List<E> args) {
        for (int i = element.size() - 1; i > -1; i--){
            int c = (int) element.get(i);
            for (int j = args.size() - 1; j > -1; j--){
                int z = (int) args.get(j);
                if (z == c){
                    args.remove(j);
                    element.remove(i);
                    break;
                }
            }
        }
    }
    
    public List<User> readUser(String json) {
        String str = json.substring(2, json.length() - 2);
        str = str.replaceAll("\"", "");
        str = str.replaceAll("\'", "");
        
        int i = str.indexOf("},{");
        while (i != -1){
            str = str.substring(0, i) + "}" + str.substring(i + 3, str.length());
            i = str.indexOf("},{");
        }
        
        String[] s, s1, str2 = str.split("}");
        int n, s11, j = str2.length - 1;
        
        List<User> uList = Lists.newArrayList();
        while (j > -1){
            User user = new User();
            s = str2[j].split(",");
            n = s.length - 1;
            while (n > -1){
                s1 = s[n].split(":");
                s11 = s1.length - 1;
                while (s11 > -1){
                    if (s1[s11].equals("id")){
                        user.setId(s1[s11 + 1]);
                        break;
                    }
                    if (s1[s11].equals("username")){
                        user.setUsername(s1[s11 + 1]);
                        break;
                    }
                    if (s1[s11].equals("sex")){
                        user.setSex(s1[s11 + 1]);
                        break;
                    }
                    if (s1[s11].equals("city")){
                        user.setCity(s1[s11 + 1]);
                        break;
                    }
                    if (s1[s11].equals("sign")){
                        user.setSign(s1[s11 + 1]);
                        break;
                    }
                    if (s1[s11].equals("experience")){
                        user.setExperience(Double.valueOf(s1[s11 + 1]));
                        break;
                    }
                    if (s1[s11].equals("logins")){
                        user.setLogins(Integer.parseInt(s1[s11 + 1]));
                        break;
                    }
                    if (s1[s11].equals("wealth")){
                        user.setWealth(Double.valueOf(s1[s11 + 1]));
                        break;
                    }
                    if (s1[s11].equals("classify")){
                        user.setClassify(s1[s11 + 1]);
                        break;
                    }
                    if (s1[s11].equals("score")){
                        user.setScore(Integer.parseInt(s1[s11 + 1]));
                        break;
                    }
                    s11--;
                }
                n--;
            }
            uList.add(user);
            j--;
        }
        return uList;
    }
    
    @SuppressWarnings({ "rawtypes", "hiding" })
    public <T> void exportExcel(List<T> data, Class args, String file) {
        if (args == null) return;
        file = file == null || file.equals("") ? "C:\\Users\\Administrator\\Desktop\\user.xlsx" : file;
        try{
//            ExcelUtils.getInstance().exportObjects2Excel(data, args, true, null, true, file);
        }
        catch (Exception e){
            // TODO Auto-generated catch block
            // e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        Properties prop = new Properties();
        try{
            // 读取属性文件a.properties
            InputStream in = new BufferedInputStream(new FileInputStream("src/config/db.properties"));
            prop.load(in); /// 加载属性列表
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()){
                String key = it.next();
                System.out.println(key + ":" + prop.getProperty(key));
            }
            in.close();
            
            /// 保存属性到b.properties文件
            FileOutputStream oFile = new FileOutputStream("b.properties", true);
            // true表示追加打开
            prop.setProperty("phone", "10086");
            prop.store(oFile, "The New properties file");
            oFile.close();
        }
        catch (Exception e){
            System.out.println(e);
        }
        
        List<String> str = new ArrayList<>();
        str.add("driver");
        str.add("url");
        str.add("username");
        str.add("password");
        
    }
}
